package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Equipment;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.EquipmentService;
import org.kjtc.util.Tools;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EquipmentController extends BaseController {

	@Resource(name = "equipmentService")
	private EquipmentService equipmentService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@RequestMapping(value = "/searchEquipmentInfo")
	@ResponseBody
	public Map<String, Object> searchEquipmentInfo(Equipment equipment, String page, String rows){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩信息查询开始");

		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		// 获取所有设备数据
		List<Equipment> list = null;
		List<Equipment> listAll = null;
		try {
			list = equipmentService.getEquipmentPage(equipment, pageQuery);
			listAll = equipmentService.getEquipment(null);
			if (listAll.size() > 0) {
				int activeCount = 0;
				int powerTypeOne = 0;
				int powerTypeOTwo = 0;
				for (Equipment equipmentAll : listAll) {
					if ("1".equals(equipmentAll.getActive())) {
						activeCount++;
					}
					if ("1".equals(equipmentAll.getEquipmentPowerType())) {
						powerTypeOne++;
					} else if ("2".equals(equipmentAll.getEquipmentPowerType())) {
						powerTypeOTwo++;
					}
				}
				map.put("activeCount", activeCount);
				map.put("notActiveCount", listAll.size() - activeCount);
				map.put("powerTypeOne", powerTypeOne);
				map.put("powerTypeOTwo", powerTypeOTwo);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩信息查询异常" + e.getMessage());
			return map;
		}

		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("equipmentList", list);

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩信息查询结束");
		equipment = null;
		return map;
	}

	@RequestMapping(value = "/searchEquipmentDetail")
	@ResponseBody
	public Map<String, Object> searchEquipmentDetail(Equipment equipment){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩详细查询开始");

		Equipment equipmentDetail = null;
		try {
			if ((equipment.getEquipmentID() != null && !equipment.getEquipmentID().isEmpty()) || (equipment.getEquipmentNo() != null && !equipment.getEquipmentNo().isEmpty())) {
				equipmentDetail = equipmentService.getEquipmentDetail(equipment);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩详细查询异常" + e.getMessage());
			return map;
		}

		map.put("equipmentDetail", equipmentDetail);

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "充电桩详细查询结束");
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveEquipmentInfo")
	public Map<String, Object> saveEquipmentInfo(Equipment equipment){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息添加开始");
		equipment.setManufactureDTTM(Tools.str2Date(equipment.getManufactureDate()));
		equipment.setCreateUser(this.getUesr().getUserID());
		equipment.setUpdateUser(this.getUesr().getUserID());
//		equipment.setEquipmentType(equipment.getAgreementID());
		int count = 0;
		try {
			count = equipmentService.insertEquipment(equipment);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex){
			String[] error = ex.getCause().getMessage().split("'");
			if ("EquipmentName".equals(error[3].trim())) {
				map.put("result", "EquipmentNameExist");
			} else if ("EquipmentNo".equals(error[3].trim())){
				map.put("result", "EquipmentNoExist");
			} else if ("EquipmentIP".equals(error[3].trim())) {
				map.put("result", "IPandPortExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息添加异常");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备基础信息添加");
		equipment = null;
		return map;
	}

	@RequestMapping(value = "/updateEquipmentInfo")
	@ResponseBody
	public Map<String, Object> updateEquipment(Equipment equipment) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息修改开始");
		equipment.setManufactureDTTM(Tools.str2Date(equipment.getManufactureDate()));
		equipment.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = equipmentService.updateEquipment(equipment);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex){
			String[] error = ex.getCause().getMessage().split("'");
			if ("EquipmentName".equals(error[3].trim())) {
				map.put("result", "EquipmentNameExist");
			} else if ("EquipmentNo".equals(error[3].trim())){
				map.put("result", "EquipmentNoExist");
			} else if ("EquipmentIP".equals(error[3].trim())) {
				map.put("result", "IPandPortExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息修改异常");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备基础信息修改");
		equipment = null;
		return map;
	}

	@RequestMapping(value = "/deleteEquipmentInfo")
	@ResponseBody
	public Object deleteEquipmentInfo(String equipmentID) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息删除开始");
		String[] equipmentIDList = equipmentID.split(",");
		Equipment equipment = new Equipment();
		equipment.setEquipmentIDList(equipmentIDList);
		equipment.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = equipmentService.deleteEquipment(equipment);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息删除结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "设备基础信息删除");
		equipment = null;
		return map;
	}
}
